export const vs_source = `
  precision mediump int;
  attribute vec4 a_Position;
  uniform float a_PointSize;
  attribute vec2 aTex;
  uniform mat4 u_proj;
  uniform mat4 u_mvp;
  varying vec2 vTex;
  uniform int isColor;
  void main() {
    gl_Position = u_proj * u_mvp * a_Position;
    gl_PointSize = a_PointSize;
    if (0==isColor)
	  vTex = aTex;
  }
`;

export const fs_source = `
  precision mediump float;
  varying vec2 vTex;
  uniform sampler2D u_samp;
  uniform int isColor;
  void main() {
    if (isColor == 5)
	  gl_FragColor = vec4(0.6, 0.6, 0.6, 1.0);
	else if (isColor == 2)
      gl_FragColor = vec4(1.0, 0.1, 0.1, 0.8);
	else if(isColor == 0)
      gl_FragColor = texture2D(u_samp, vTex);
	else if (isColor == 1)
      gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);
	else {
      vec2 texCoor = gl_PointCoord;
      gl_FragColor = texture2D(u_samp, 0.046875*texCoor+vec2(0.46875, 0.0));
	  if (isColor == 4)
        gl_FragColor.rb = vec2(0.0, 0.0);
	}
  }
`;

export const tempbuffer = new Float32Array(16);

export function loadShader(gl, type, source) {
  let shader = gl.createShader(type);
  gl.shaderSource(shader, source);
  gl.compileShader(shader);
  if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
    const error = gl.getShaderInfoLog(shader);
    gl.deleteShader(shader);
    throw 'Failed to compile shader:' + error;
  }
  return shader;
}

export function loadTexture(gl, tex, img, samp) {
	gl.activeTexture(gl.TEXTURE0);
	gl.bindTexture(gl.TEXTURE_2D, tex);
    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
	gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
	gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, img);
	gl.uniform1i(samp, 0);
	gl.clear(gl.COLOR_BUFFER_BIT);
}